package com.accelstack.cmp.controller;

import com.accelstack.cmp.entity.GpuModel;
import com.accelstack.cmp.service.GpuModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * GPU型号管理控制器
 */
@Tag(name = "GPU型号管理", description = "GPU型号的查询、创建、更新和删除接口")
@RestController
@RequestMapping("/api/gpu-models")
@RequiredArgsConstructor
public class GpuModelController {
    
    private final GpuModelService gpuModelService;
    
    @Operation(summary = "查询所有GPU型号", description = "获取所有GPU型号列表")
    @GetMapping("/all")
    public ResponseEntity<List<GpuModel>> getAll() {
        List<GpuModel> gpuModels = gpuModelService.findAll();
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "分页查询GPU型号", description = "分页查询GPU型号列表")
    @GetMapping
    public ResponseEntity<Page<GpuModel>> getPage(
            @PageableDefault(size = 20, sort = "id") Pageable pageable) {
        Page<GpuModel> gpuModels = gpuModelService.findAll(pageable);
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "根据ID查询GPU型号", description = "根据ID查询单个GPU型号详情")
    @GetMapping("/{id}")
    public ResponseEntity<GpuModel> getById(
            @Parameter(description = "GPU型号ID", required = true) @PathVariable Long id) {
        return gpuModelService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @Operation(summary = "创建GPU型号", description = "创建新的GPU型号")
    @PostMapping
    public ResponseEntity<GpuModel> create(@Valid @RequestBody GpuModel gpuModel) {
        gpuModel.setCreatedAt(new Date());
        GpuModel created = gpuModelService.create(gpuModel);
        return ResponseEntity.ok(created);
    }
    
    @Operation(summary = "更新GPU型号", description = "更新指定ID的GPU型号信息")
    @PutMapping("/{id}")
    public ResponseEntity<GpuModel> update(
            @Parameter(description = "GPU型号ID", required = true) @PathVariable Long id,
            @Valid @RequestBody GpuModel gpuModel) {
        return gpuModelService.findById(id)
                .map(existing -> {
                    gpuModel.setId(id);
                    gpuModel.setCreatedAt(existing.getCreatedAt());
                    gpuModel.setUpdatedAt(new Date());
                    GpuModel updated = gpuModelService.update(gpuModel);
                    return ResponseEntity.ok(updated);
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    @Operation(summary = "删除GPU型号", description = "根据ID删除GPU型号")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(
            @Parameter(description = "GPU型号ID", required = true) @PathVariable Long id) {
        return gpuModelService.findById(id)
                .map(gpuModel -> {
                    gpuModelService.deleteById(id);
                    return ResponseEntity.ok().<Void>build();
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    @Operation(summary = "批量删除GPU型号", description = "根据ID列表批量删除GPU型号")
    @DeleteMapping("/batch")
    public ResponseEntity<String> batchDelete(@RequestBody List<Long> ids) {
        gpuModelService.batchDelete(ids);
        return ResponseEntity.ok("批量删除成功，共删除 " + ids.size() + " 个GPU型号");
    }
    
    @Operation(summary = "根据厂商查询GPU型号", description = "根据厂商名称查询GPU型号列表")
    @GetMapping("/vendor/{vendor}")
    public ResponseEntity<List<GpuModel>> getByVendor(
            @Parameter(description = "厂商名称", required = true) @PathVariable String vendor) {
        List<GpuModel> gpuModels = gpuModelService.findByVendor(vendor);
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "获取所有厂商列表", description = "获取系统中所有GPU厂商的列表")
    @GetMapping("/vendors")
    public ResponseEntity<List<String>> getAllVendors() {
        List<String> vendors = gpuModelService.findAllVendors();
        return ResponseEntity.ok(vendors);
    }
    
    @Operation(summary = "根据名称搜索GPU型号", description = "模糊搜索GPU型号名称")
    @GetMapping("/search")
    public ResponseEntity<List<GpuModel>> searchByName(
            @Parameter(description = "搜索关键词", required = true) @RequestParam String keyword) {
        List<GpuModel> gpuModels = gpuModelService.searchByName(keyword);
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "根据显存大小查询GPU型号", description = "查询指定显存大小的GPU型号")
    @GetMapping("/vram/{vramGb}")
    public ResponseEntity<List<GpuModel>> getByVram(
            @Parameter(description = "显存大小(GB)", required = true) @PathVariable Integer vramGb) {
        List<GpuModel> gpuModels = gpuModelService.findByVramGb(vramGb);
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "根据显存范围查询GPU型号", description = "查询指定显存范围内的GPU型号")
    @GetMapping("/vram/range")
    public ResponseEntity<List<GpuModel>> getByVramRange(
            @Parameter(description = "最小显存(GB)", required = true) @RequestParam Integer minVram,
            @Parameter(description = "最大显存(GB)", required = true) @RequestParam Integer maxVram) {
        List<GpuModel> gpuModels = gpuModelService.findByVramRange(minVram, maxVram);
        return ResponseEntity.ok(gpuModels);
    }
    
    @Operation(summary = "批量创建GPU型号", description = "批量创建多个GPU型号")
    @PostMapping("/batch")
    public ResponseEntity<List<GpuModel>> batchCreate(@Valid @RequestBody List<GpuModel> gpuModels) {
        Date now = new Date();
        gpuModels.forEach(gpu -> gpu.setCreatedAt(now));
        List<GpuModel> created = gpuModelService.batchCreate(gpuModels);
        return ResponseEntity.ok(created);
    }
    
    @Operation(summary = "获取GPU型号统计信息", description = "获取各厂商GPU型号数量统计")
    @GetMapping("/statistics")
    public ResponseEntity<?> getStatistics() {
        return ResponseEntity.ok(gpuModelService.getStatistics());
    }
}

